package com.miku.project.job;

import com.miku.mikuapicommon.model.entity.ProductOrder;
import com.miku.project.service.OrderService;
import com.miku.project.service.ProductOrderService;
import com.miku.project.utils.RedissonLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;

import static com.miku.project.model.enums.PayTypeStatusEnum.ALIPAY;
import static com.miku.project.model.enums.PayTypeStatusEnum.WX;

/**
 * 处理超时订单
 */
@Slf4j
@Component
public class PayJob {
    @Resource
    private RedissonLockUtil redissonLockUtil;
    @Resource
    private OrderService orderService;
    @Resource
    private ProductOrderService productOrderService;

    /**
     * 微信订单确认
     * 每20s查询一次超过5分钟过期的订单,并且未支付
     */
    @Scheduled(cron = "0/25 * * * * ?")
    public void WxPayOrderConfim(){
        redissonLockUtil.redissonDistributedLocks("WxPayOrderConfirm",()->{

            //查询超过5分钟过期的订单,并且未支付
            List<ProductOrder> orderList=orderService.getNoPayOrderByDuration(5,false,WX.getValue());
            ProductOrderService productOrderService = orderService.getProductOrderServiceByPayType(WX.getValue());
            for (ProductOrder productOrder : orderList) {
                String orderNo = productOrder.getOrderNo();
                try {
                    productOrderService.processingTimedOutOrders(productOrder);
                } catch (Exception e) {
                    log.error("微信超时订单,{},确认异常",orderNo,e);
                    break;
                }
            }
        });
    }

    /**
     * 支付宝订单确认
     * 每20s查询一次超过5分钟过期的订单,并且未支付
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void aliPayOrderConfim(){
        redissonLockUtil.redissonDistributedLocks("aliPayOrderConfirm",()->{

            //查询超过5分钟过期的订单,并且未支付
            List<ProductOrder> orderList=orderService.getNoPayOrderByDuration(5,false,ALIPAY.getValue());
            ProductOrderService productOrderService = orderService.getProductOrderServiceByPayType(ALIPAY.getValue());
            for (ProductOrder productOrder : orderList) {
                String orderNo = productOrder.getOrderNo();
                try {
                    productOrderService.processingTimedOutOrders(productOrder);
                } catch (Exception e) {
                    log.error("支付宝超时订单,{},确认异常",orderNo,e);
                    break;
                }
            }
        });
    }


    /**
     * 订单确认
     * 每2点删除一次15天以前的订单,并且未支付,并且已关闭的订单
     */
    @Scheduled(cron = "* * 2 * * ?")
    public void  clearOverdueOrders(){
       redissonLockUtil.redissonDistributedLocks("clearOverdueOrders",()-> {

           List<ProductOrder> orderList = orderService.getNoPayOrderByDuration(15 * 24 * 60, true, "");
           boolean removeBatchByIds = productOrderService.removeBatchByIds(orderList);
           if (removeBatchByIds){
               log.info("删除超时订单成功");
           }
       });
  }
}
